home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.10 Oct 96 / EvansJavaNet / JavalogClient.java < prev    next >
Encoding:
Java Source  |  1996-06-14  |  1.4 KB  |  53 lines  |  [TEXT/Rstr]

  1. /*
  2.     JavalogClient.java
  3.     
  4.     A very basic implemtation of a syslog message dispatcher in Java 
  5.     using UDP datagram sockets
  6.     
  7.     By Christopher Evans
  8.     Copyright © 1996 Natural Intelligence, Inc.
  9. */
  10.  
  11. import java.net.*;
  12. import java.io.*;
  13.  
  14. /**
  15.     Implements a basic syslog client, sending data to port 514 on
  16.     the machine whose name is passed in as a command line argument
  17. */ 
  18.  
  19. public class JavalogClient {
  20.  
  21.     public static void main(String args[]) throws Exception
  22.     {
  23.         if(args.length != 2) {
  24.             System.out.println("Usage: JavalogClient <host> <message>");
  25.             System.exit(0);
  26.         }
  27.         
  28.         //Create an InetAddress object and initialize it from the first argument
  29.         //which should be a host name like bart.natural.com
  30.         InetAddress    address = InetAddress.getByName(args[0]);
  31.         
  32.         //Find out how long the message is, then copy it from a java.lang.String
  33.         //into an array of bytes to be sent.
  34.         int msg_len = args[1].length();
  35.         
  36.         byte[]    message = new byte[msg_len];
  37.         
  38.         args[1].getBytes(0,msg_len, message,0);
  39.         
  40.         //Create a DatagramPacket object with the data that the user wants to send
  41.         DatagramPacket    packet = new DatagramPacket(message, msg_len, address, 514);
  42.         
  43.         //Create a new datagram socket to send the packet
  44.         DatagramSocket    socket = new DatagramSocket();
  45.         
  46.         //Now actually send the data across the network
  47.         socket.send(packet);
  48.         
  49.         //clean up the socket so it isn't left open
  50.         socket.close();
  51.         
  52.     }
  53. }